package com.zjitc.vo;

import com.zjitc.utils.MyNumber;
import java.util.List;
import org.apache.ibatis.annotations.Param;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/23 16:55
 * @description:
 */
public class PageModel<T> {


  /**
   * 当前页
   */
  private int page;
  /**
   * 总页数
   */
  private int totalPages;
  /**
   * 每页12条数据
   */
  private int pageRecorders = 12;
  /**
   * 总数据数
   */
  private int totalRows;
  /**
   * 每页的起始数
   */
  private int pageStartRow;
  /**
   * 每页显示数据的终止数
   */
  private int pageEndRow;
  /**
   * 导航条上的第一页
   */
  private int navigateFirstPage;
  /**
   * 导航条上的最后一页
   */
  private int navigateLastPage;
  /**
   * 导航条固定 5页
   */
  private int navigatePageNum = 5;

  private int prePage;

  private int nextPage;

  private List<T> alllist;

  private List<T> list;

  private Integer size;


  public PageModel(List list, int pageRecorders, @Param("pageid") int pageid) {
    if (pageid <= 0) {
      pageid = 1;
    }
    init(list, pageRecorders, pageid);
    // 通过对象集，记录总数划分
  }

  public PageModel(List list, int pageid) {
    if (pageid <= 0) {
      pageid = 1;
    }
    init(list, pageRecorders, pageid);
    // 通过对象集，记录总数划分
  }


  /**
   * 初始化list，并告之该list每页的记录数
   */
  public void init(List list, int pageRecorders, int pageid) {
    this.pageRecorders = pageRecorders;
    this.alllist = list;
    this.page = pageid;
    totalRows = list.size();

    if ((totalRows % pageRecorders) == 0) {
      totalPages = totalRows / pageRecorders;
    } else {
      totalPages = totalRows / pageRecorders + 1;
    }

    if (page >= totalPages) {
      page = totalPages;
    }

    makepreandnext();
    makenavigate();

    if (totalRows < pageRecorders) {
      this.pageStartRow = 0;
      this.pageEndRow = totalRows;
    } else {
      this.pageStartRow = (page - 1) * pageRecorders;
      this.pageEndRow = page * pageRecorders;
    }
    if (this.pageEndRow > totalRows) {
      pageEndRow = totalRows;
    }
    this.list = alllist.subList(pageStartRow, pageEndRow);
    this.size = list.size();
  }

  /**
   * 分页导航条
   */
  private void makenavigate() {
    if (totalPages != 1) {
      this.navigateFirstPage = page - MyNumber.TWO.ordinal();
      while (this.navigateFirstPage <= MyNumber.ZERO.ordinal()) {
        navigateFirstPage++;
      }
      navigateLastPage = navigateFirstPage + navigatePageNum;
      while (this.navigateLastPage > this.totalPages) {
        navigateLastPage--;
      }
      while (navigateLastPage - navigateFirstPage < MyNumber.FOUR.ordinal()
          && navigateFirstPage > MyNumber.ONE.ordinal()) {
        navigateFirstPage--;
      }
    } else {
      navigateFirstPage = totalPages;
      navigateLastPage = totalPages;
    }

  }


  /**
   * 保证上一页下一页不会错误
   */
  public void makepreandnext() {
    this.prePage = page - 1;
    this.nextPage = page + 1;

    if (prePage <= 0) {
      this.prePage = 1;
    }
    if (nextPage >= totalPages) {
      this.nextPage = totalPages;
    }
  }

  public int getPrePage() {
    return prePage;
  }

  public void setPrePage(int prePage) {
    this.prePage = prePage;
  }

  public int getNextPage() {
    return nextPage;
  }

  public void setNextPage(int nextPage) {
    this.nextPage = nextPage;
  }

  public List getList() {
    return list;
  }

  public List getAlllist() {
    return alllist;
  }

  public void setAlllist(List<T> alllist) {
    this.alllist = alllist;
  }

  public void setList(List<T> list) {
    this.list = list;
  }

  public int getPage() {
    return page;
  }

  public void setPage(int page) {
    this.page = page;
  }

  public int getPageEndRow() {
    return pageEndRow;
  }

  public void setPageEndRow(int pageEndRow) {
    this.pageEndRow = pageEndRow;
  }

  public int getPageRecorders() {
    return pageRecorders;
  }

  public void setPageRecorders(int pageRecorders) {
    this.pageRecorders = pageRecorders;
  }

  public int getPageStartRow() {
    return pageStartRow;
  }

  public void setPageStartRow(int pageStartRow) {
    this.pageStartRow = pageStartRow;
  }

  public int getTotalPages() {
    return totalPages;
  }

  public void setTotalPages(int totalPages) {
    this.totalPages = totalPages;
  }

  public int getTotalRows() {
    return totalRows;
  }

  public void setTotalRows(int totalRows) {
    this.totalRows = totalRows;
  }

  public int getNavigateFirstPage() {
    return navigateFirstPage;
  }

  public void setNavigateFirstPage(int navigateFirstPage) {
    this.navigateFirstPage = navigateFirstPage;
  }

  public int getNavigateLastPage() {
    return navigateLastPage;
  }

  public void setNavigateLastPage(int navigateLastPage) {
    this.navigateLastPage = navigateLastPage;
  }

  public int getNavigatePageNum() {
    return navigatePageNum;
  }

  public void setNavigatePageNum(int navigatePageNum) {
    this.navigatePageNum = navigatePageNum;
  }

  public Integer getSize() {
    return size;
  }

  public void setSize(Integer size) {
    this.size = size;
  }
}
